package view
{	
	import events.*;
	
	import flash.display.StageDisplayState;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import models.Model;
	
	import skins.SlideShowPreviewSkin;
	
	import spark.components.Button;
	import spark.components.supportClasses.SkinnableComponent;
	
	[Event(name="moveFocus", type="events.MoveFocusEvent")]
	[Event(name="changeStates", type="events.ChangeStatesEvent")]	
	public class Presentation extends SkinnableComponent
	{			

		[SkinPart] public var backButton:Button;
		[SkinPart] public var pauseButton:Button;
		[SkinPart] public var playButton:Button;
		[SkinPart] public var nextButton:Button;

		[Bindable]
		public var model:Model;
		
		private var timer:Timer;
		private static const slideDelay:int = 1000;
		private var isPlaying:Boolean = false;
		public function Presentation()
		{
			super();
			timer = new Timer(slideDelay);
			timer.addEventListener(TimerEvent.TIMER,changeSlide);
		}

		protected override function childrenCreated():void
		{
			super.childrenCreated();
			parentApplication.stage.displayState = StageDisplayState.FULL_SCREEN;
			this.parentApplication.stage.addEventListener(KeyboardEvent.KEY_UP, escape);
		}
		override protected function partAdded(partName:String, instance:Object):void
		{
			super.partAdded(partName, instance);
			
			if (instance == backButton)
			{
				backButton.addEventListener(MouseEvent.CLICK, handleMoveFocusLeftClick);				
			}
			if (instance == nextButton)
			{
				nextButton.addEventListener(MouseEvent.CLICK, handleMoveFocusRightClick);		
			}
			if (instance == playButton)
			{
				playButton.addEventListener(MouseEvent.CLICK, handlePlayClick);	
			}
			if (instance == pauseButton)
			{
				pauseButton.addEventListener(MouseEvent.CLICK, handlePauseClick);	
			}
		}		
		
		private function handlePlayClick(event:MouseEvent):void
		{
			timer.repeatCount += model.slidesLeft;			
			timer.start();
		}
		private function handlePauseClick(event:MouseEvent):void
		{
			timer.stop();
		}	
		private function changeSlide(event:TimerEvent):void
		{
			dispatchEvent(new MoveFocusEvent(MoveFocusEvent.MOVE,"right" ));
		}
		
		public function escape(event:KeyboardEvent):void
		{
			if (event.keyCode == 27)
			{
				dispatchEvent(new ChangeStatesEvent(ChangeStatesEvent.CHANGE,"BuildSlideShow"));	
			}
			if(event.keyCode==37)
			{
				dispatchEvent(new MoveFocusEvent(MoveFocusEvent.MOVE,"left"));
			}
			if(event.keyCode==39)
			{
				dispatchEvent(new MoveFocusEvent(MoveFocusEvent.MOVE,"right" ));
			}
			if(event.keyCode==32)
			{
				if (!isPlaying)	
				{
					timer.repeatCount += model.slidesLeft;			
					timer.start();
					isPlaying = true;
				}
				else
				{
					timer.stop();
					isPlaying = false;
				}
			}
		}			
		private function handleMoveFocusLeftClick(event:MouseEvent):void
		{
			dispatchEvent(new MoveFocusEvent(MoveFocusEvent.MOVE,"left"));
		}
		private function handleMoveFocusRightClick(event:MouseEvent):void
		{
			dispatchEvent(new MoveFocusEvent(MoveFocusEvent.MOVE,"right" ));
		}
		
	}
}